#include "App_LoRa_Proto.h"
#include "App_Modbus_Proto.h"
#include "App_Send_Data_Task.h"
#include "ads1115-board.h"
#include "board.h"
#include "log.h"
#include "system.h"
#include "usart-board.h"

#if defined(CONFIG_DTU_485)
int App_485_AcqData(uint8_t out[])
{
    for (int i = 0; i < SYSTEM_DEVICE_NUM; i++) {
        LoraDevice_t device = SystemParmData.loraDevice[i];
        if (device.modelId == 0 || SystemLoRaData.rs485TxQueue == NULL) {
            continue;
        }
        LOG_PRINTF(LL_DEBUG, "Notify device acquire, index = %d\r\n", i);
        xQueueSend(SystemLoRaData.rs485TxQueue, &device, portMAX_DELAY);
    }
    // AppModbusProto_t appModbusProto = {0}
    return 0;
}
#endif

#if defined(CONFIG_DTU_AI)
int App_AI_GetResult(uint8_t out[])
{
    SystemLoRaData.aiChannelConfig[0] = ADS1115_CHANNEL1_CONFIG;
    SystemLoRaData.aiChannelConfig[1] = ADS1115_CHANNEL2_CONFIG;
    SystemLoRaData.aiChannelConfig[2] = ADS1115_CHANNEL3_CONFIG;
    SystemLoRaData.aiChannelConfig[3] = ADS1115_CHANNEL4_CONFIG;

    int index = 0;
    int i;
    for (i = 0; i < SYSTEM_AI_ATTRIBUTE_NUM; i++) {
        if (SystemParmData.aiAttrData[i].attrId == 0) {
            continue;
        }
        SystemLoRaData.aiAttrSampleData[i].attrData = BoardAds1115Result(SystemLoRaData.aiChannelConfig[i]);
        SystemLoRaData.systemState.flagBit.aiSampleFirst = 1;

        uint32_t len = 4;
        Fill_Data_1B(out, &index, SystemParmData.aiAttrData[i].attrId);
        Fill_Data_1B(out, &index, len);
        Fill_Data(out, &index, (uint8_t*)&SystemLoRaData.aiAttrSampleData[i].attrData, len);
    }
    return index;
}
#endif

#if defined(CONFIG_DTU_DOUT)
int App_DOUT_SetResult(uint8_t out[])
{
    int i;
    int index = 0;
    for (i = 0; i < SYSTEM_DOUT_ATTRIBUTE_NUM; i++) {
        if (SystemParmData.doutAttrData[i].attrId != 0) {
            SystemDoutModeSet(SystemParmData.doutAttrData[i].attrId, SystemParmData.doutAttrData[i].attrData);
            Fill_Data_1B(out, &index, SystemParmData.doutAttrData[i].attrId);
            Fill_Data_1B(out, &index, (uint8_t)1);
            Fill_Data_1B(out, &index, ((SystemParmData.doutAttrData[i].attrData & 0x000000FF) >> 0) & 0xFF);
        }
    }
    return index;
}
#endif

#if defined(CONFIG_DTU_DIN)
int App_DIN_SetResult(uint8_t out[])
{
    int index = 0;
    int i;
    for (i = 0; i < SYSTEM_DIN_ATTRIBUTE_NUM; i++) {
        if (SystemParmData.dinAttrData[i].attrId == 0) {
            continue;
        }
        SystemLoRaData.dinAttrSampleData[i].attrData = SystemDinModeGet(SystemParmData.dinAttrData[i].attrId);
        SystemLoRaData.systemState.flagBit.dinSampleFirst = 1;
        Fill_Data_1B(out, &index, SystemParmData.doutAttrData[i].attrId);
        Fill_Data_1B(out, &index, (uint8_t)1);
        Fill_Data_1B(out, &index, ((SystemLoRaData.dinAttrSampleData[i].attrData & 0x000000FF) >> 0) & 0xFF);
    }
    return index;
}
#endif

void App_Send_Data_Check_Task(void* pvParameters)
{
    uint32_t times = 0;
    uint8_t sendFlag = 0;
    uint8_t alarmRecoveryFlag = 0;
    uint8_t out[256] = {0};

    while (1) {
        // LOG_PRINTF(LL_DEBUG, "Lora Data Cycle Sleep\r\n");
        vTaskDelay(APP_SEND_DATA_TASK_DELAY_MS);
        WatchDogToggle();
        if ((((++times) * APP_SEND_DATA_TASK_DELAY_MS) % (SystemParmData.txDutyCycleTime * 1000)) != 0) {
            continue;
        }

        if ((SystemLoRaData.systemState.flagBit.loraJoinSuccess != 1) ||
            (SystemLoRaData.systemState.flagBit.loraDeviceTime != 1)) {
            continue;
        }

        LOG_PRINTF(LL_DEBUG, "Lora Data Cycle\r\n");
#if defined(CONFIG_DTU_485)
        do {
            int len = App_485_AcqData(out);
            // if((SystemLoRaData.loraTxMutex != NULL) && (SystemLoRaData.systemState.flagBit.loraMutexState == 0)) {
            //     xSemaphoreTake(SystemLoRaData.loraTxMutex, portMAX_DELAY);
            //     SystemLoRaData.systemState.flagBit.loraMutexState = 1;
            //     LOG_PRINTF(LL_DEBUG, "--------------SendNomalData:App_485_AcqData-------------\r\n");
            //     App_Lora_Proto_SendData(out, len);
            // }
        } while (0);
#endif

#if defined(CONFIG_DTU_AI)
        do {
            int len = App_AI_GetResult(out);
            if (len > 0 && (SystemLoRaData.systemState.flagBit.loraMutexState == 0)) {
                LOG_PRINTF(LL_DEBUG, "--------------SendNomalData:App_AI_GetResult-------------\r\n");
                App_Lora_Proto_SendData(out, len);
            }
        } while (0);
#endif

#if defined(CONFIG_DTU_DOUT)
        do {
            int len = App_DOUT_SetResult(out);
            if (len > 0 && (SystemLoRaData.systemState.flagBit.loraMutexState == 0)) {
                LOG_PRINTF(LL_DEBUG, "--------------SendNomalData:App_DOUT_SetResult-------------\r\n");
                App_Lora_Proto_SendData(out, len);
            }
        } while (0);
#endif

#if defined(CONFIG_DTU_DIN)
        do {
            int len = App_DIN_SetResult(out);
            if (len > 0 && (SystemLoRaData.systemState.flagBit.loraMutexState == 0)) {
                LOG_PRINTF(LL_DEBUG, "--------------SendNomalData:App_DIN_SetResult-------------\r\n");
                App_Lora_Proto_SendData(out, len);
            }
        } while (0);
#endif
    }
}
